package moodish.client;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;

import javax.swing.AbstractListModel;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;

import moodish.client.FriendsPanel.FriendsCellRenderer;
import moodish.client.FriendsPanel.FriendsListModel;

/**
 * This class represents the Online Users panel that is added to the ClientGUI.
 * 
 * @author es1-2013-group2
 *
 */
public class OnlineUsersPanel extends JPanel{

	private JList usersList;
	private DefaultListModel usersListModel;
	private JButton friendButton;

	/**
	 * List of online Users.
	 */
	private ArrayList<User> users;
	private ClientGUI clientGUI;

	/**
	 * 
	 * Constructs a new OnlineUsersPanel with the parameter ClientGUI.
	 * Sets the layout of this panel.
	 * The actionListener sets the selected User as a friend.
	 * 
	 * @param clientGUI.
	 */
	public OnlineUsersPanel(final ClientGUI clientGUI){
		this.clientGUI = clientGUI;
		users = new ArrayList<User>();

		usersListModel = new DefaultListModel();
		usersList = new JList(usersListModel);
		usersList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		usersList.setVisibleRowCount(5);
		User protoCellValue = new User("asdasdasfa", Mood.Confused);
		usersList.setPrototypeCellValue(protoCellValue);
		JScrollPane listScrollPane = new JScrollPane(usersList);

		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		this.add(listScrollPane);
		this.setBorder(BorderFactory.createTitledBorder("Users"));

		friendButton = new JButton("Friend");
		friendButton.setEnabled(false);
		friendButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				int index = usersList.getSelectedIndex();

				User friend = null;
				for(User user: users){
					if(user.getNickname() == usersList.getSelectedValue()){
						friend = user;
					}
				}
				// add selected user to friends list (ClientGUI)
				clientGUI.addFriend(friend);

				usersListModel.remove(index);
				usersList.updateUI();

				int size = usersListModel.getSize();

				if (size == 0) { //Nobody's left, disable unfriend.
					friendButton.setEnabled(false);

				} else { //Select an index.
					if (index == usersListModel.getSize()) {
						//removed item in last position
						index--;
					}

					usersList.setSelectedIndex(index);
					usersList.ensureIndexIsVisible(index);
				}
			}
		});

		this.add(friendButton);
	}


	/**
	 * Add a online User to the list.
	 * Add the User nickname to the usersListModel.
	 * 
	 * @param user User that will be added to the list.
	 */
	public void addUser(User user) {
		users.add(user);
		usersListModel.addElement(user.getNickname());
		usersList.updateUI();
		if(!friendButton.isEnabled()){
			System.out.println("friend button activo");
			friendButton.setEnabled(true);
		}
		usersList.setSelectedIndex(0);
		System.out.println("Adicionei o user " + user.getNickname());

	}

	/**
	 * Remove the nickname from the online users list.
	 *  
	 * @param nickname 
	 */
	public void removeUser(String nickname) {

		int index = -1;

		for (int i = 0; i < usersListModel.getSize(); i++) {
			if(usersListModel.getElementAt(i).equals(nickname))
				index = i;
		}
		if(index!=-1){
			usersListModel.remove(index);
			usersList.updateUI();
			if(usersListModel.isEmpty())
				friendButton.setEnabled(false);
		}
	}



	/**
	 * Remove all users from the online users list.
	 * 
	 */
	public void removeAllUsers(){
		users = new ArrayList<User>();
		usersListModel.removeAllElements();
		usersList.updateUI();
		friendButton.setEnabled(false);
	}




}
